<template>
  <div class="toolbar">
    <el-tooltip
      placement="bottom"
      v-for="command in commands"
      :key="command.name"
      :content="command.title"
    >
      <div class="item" @click="handleCommandClick(command.name)">
        <span><i class="iconfont" :class="command.icon"></i></span>
      </div>
    </el-tooltip>
  </div>
</template>

<script>
import commands from "./commands";
import EventBus from "../../event-bus";
import BusEvents from "../../event-bus/event-types";

export default {
  name: "TopToolbar",
  props: {
    mode: String,
  },
  data() {
    return {
      commands,
    };
  },
  computed: {
    modeName() {
      return this.mode === "editing" ? "编辑中..." : "查看中...";
    },
  },
  methods: {
    handleCommandClick(command) {
      switch (command) {
        case "undo":
          EventBus.$emit(BusEvents.COMMAND_UNDO);
          break;
        case "redo":
          EventBus.$emit(BusEvents.COMMAND_REDO);
          break;
        case "copy":
          EventBus.$emit(BusEvents.COMMAND_COPY);
          break;
        case "cut":
          EventBus.$emit(BusEvents.COMMAND_CUT);
          break;
        case "paste":
          EventBus.$emit(BusEvents.COMMAND_PASTE);
          break;
        case "delete":
          EventBus.$emit(BusEvents.COMMAND_DELETE);
          break;
        case "save":
          EventBus.$emit(BusEvents.COMMAND_SAVE);
          break;
        default:
          break;
      }
    },
  },
};
</script>

<style lang="less" scoped>
.toolbar {
  width: 100%;
  height: 40px;
  box-sizing: border-box;
  border-bottom: 1px solid rgba(0, 0, 0, 0.08);
  padding-left: 30px;

  display: flex;
  flex-direction: row;
  align-items: center;

  .item {
    font-size: 16px;
    margin: 0 2px;
    padding: 2px 8px;
    border-radius: 2px;
    cursor: pointer;

    &:hover {
      background-color: #eeeeee;
    }
  }

  .item.last {
    margin-left: auto;
    margin-right: 10px;
    font-weight: bold;
    user-select: none;
  }
}
</style>
